package com.py.inbound.service.impl;

import java.util.Collections;
import java.util.List;

import com.py.inbound.domain.OdSaleRejected;
import com.py.inbound.domain.OdSaleRejectedDetails;
import com.py.inbound.domain.PyInboundDetalis;
import com.py.inbound.mapper.OdSaleRejectedMapper;
import com.py.inbound.mapper.PyInboundDetalisMapper;
import com.py.inbound.service.IOdSaleRejectedService;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Objects;

import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;

/**
 * 销售退货Service业务层处理
 *
 * @author 童佳
 * @date 2024-12-12
 */
@Service
public class OdSaleRejectedServiceImpl implements IOdSaleRejectedService {
    @Resource
    private OdSaleRejectedMapper odSaleRejectedMapper;
    @Resource
    private PyInboundDetalisMapper pyInboundDetalisMapper;

    /**
     * 查询销售退货
     *
     * @param srId 销售退货主键
     * @return 销售退货
     */
    @Override
    public OdSaleRejected selectOdSaleRejectedBySrId(Integer srId) {
        return odSaleRejectedMapper.selectOdSaleRejectedBySrId(srId);
    }

    /**
     * 查询销售退货列表
     *
     * @param odSaleRejected 销售退货
     * @return 销售退货
     */
    @Override
    public List<OdSaleRejected> selectOdSaleRejectedList(OdSaleRejected odSaleRejected) {
        return odSaleRejectedMapper.selectOdSaleRejectedList(odSaleRejected);
    }

    /**
     * 新增销售退货
     *
     * @param odSaleRejected 销售退货
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOdSaleRejected(OdSaleRejected odSaleRejected) {
        odSaleRejected.setCreateTime(DateUtils.getNowDate());
        int rows = odSaleRejectedMapper.insertOdSaleRejected(odSaleRejected);
        insertOdSaleRejectedDetails(odSaleRejected);
        return rows;
    }

    /**
     * 修改销售退货
     *
     * @param odSaleRejected 销售退货
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOdSaleRejected(OdSaleRejected odSaleRejected) {
        odSaleRejected.setUpdateTime(DateUtils.getNowDate());
        odSaleRejectedMapper.deleteOdSaleRejectedDetailsBySrCode(odSaleRejected.getSrId());
        insertOdSaleRejectedDetails(odSaleRejected);
        return odSaleRejectedMapper.updateOdSaleRejected(odSaleRejected);
    }

    /**
     * 批量删除销售退货
     *
     * @param srIds 需要删除的销售退货主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOdSaleRejectedBySrIds(Integer[] srIds) {
        odSaleRejectedMapper.deleteOdSaleRejectedDetailsBySrCodes(srIds);
        return odSaleRejectedMapper.deleteOdSaleRejectedBySrIds(srIds);
    }

    /**
     * 删除销售退货信息
     *
     * @param srId 销售退货主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOdSaleRejectedBySrId(Integer srId) {
        odSaleRejectedMapper.deleteOdSaleRejectedDetailsBySrCode(srId);
        return odSaleRejectedMapper.deleteOdSaleRejectedBySrId(srId);
    }

    @Override
    public List<OdSaleRejectedDetails> selectOdSaleRejectedDetailsList(OdSaleRejectedDetails odSaleRejectedDetails) {
        List<OdSaleRejectedDetails> odSaleRejectedDetails1 = odSaleRejectedMapper.selectOdSaleRejectedDetailsList(odSaleRejectedDetails);
//        System.out.println("odSaleRejectedDetails:" + odSaleRejectedDetails);
//        System.out.println("odSaleRejectedDetails1:" + odSaleRejectedDetails1);
        List<PyInboundDetalis> pyInboundDetalis1 = null;
        for (OdSaleRejectedDetails od : odSaleRejectedDetails1) {
            PyInboundDetalis pyInboundDetalis = new PyInboundDetalis();
            pyInboundDetalis.setProductCode(od.getProductCode());
            pyInboundDetalis.setPhDetailsId(od.getSrDetailsId());
            pyInboundDetalis.setPhCode(od.getSrCode());
//            System.out.println("萨达安安:" + pyInboundDetalis);
            pyInboundDetalis1 = pyInboundDetalisMapper.selectquantityReceivedPending(pyInboundDetalis);
/*            System.out.println("萨达安安02:" + pyInboundDetalis1);*/
            if (pyInboundDetalis1 == null || pyInboundDetalis1.isEmpty()) {
                od.setNotStocked(od.getRejectedNum());
                od.setAlreadyNum(0);
            }
            if (pyInboundDetalis1 != null) {
                for (PyInboundDetalis p : pyInboundDetalis1) {
                    if (Objects.equals(p.getPhDetailsId(), od.getSrDetailsId())) {
                        if (p.getQuantityReceived() == null || p.getQuantityReceived() == 0) {
                            od.setNotStocked(od.getRejectedNum());
                            od.setAlreadyNum(0);
                        } else {
                            od.setNotStocked(p.getQuantityPending());
                            od.setAlreadyNum(p.getQuantityReceived());
                        }
                    }
                }
            }

        }
        return odSaleRejectedDetails1;
    }

    /**
     * 新增${subTable.functionName}信息
     *
     * @param odSaleRejected 销售退货对象
     */
    public void insertOdSaleRejectedDetails(OdSaleRejected odSaleRejected) {
        List<OdSaleRejectedDetails> odSaleRejectedDetailsList = odSaleRejected.getOdSaleRejectedDetailsList();
        String srCode = odSaleRejected.getSrCode();
        if (StringUtils.isNotNull(odSaleRejectedDetailsList)) {
            List<OdSaleRejectedDetails> list = new ArrayList<OdSaleRejectedDetails>();
            for (OdSaleRejectedDetails odSaleRejectedDetails : odSaleRejectedDetailsList) {
                odSaleRejectedDetails.setSrCode(srCode);
                list.add(odSaleRejectedDetails);
            }
            if (list.size() > 0) {
                odSaleRejectedMapper.batchOdSaleRejectedDetails(list);
            }
        }
    }
}
